<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>
<%--
  Created by IntelliJ IDEA.
  User: bobo
  Date: 16-10-27
  Time: 下午2:30
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <jsp:include page="../layout/head.jsp" >
        <jsp:param name="title" value="类别列表" />
    </jsp:include>

    <style>
        .button{
            margin: 5px;
        }
    </style>
</head>
<body>
<div class="panel admin-panel">
    <div class="panel-head"><strong class="icon-reorder"> 类别列表</strong></div>
    <ul class="search header-search">
        <li><a class="button border-main icon-plus-square-o" href="${pageContext.request.contextPath}/admin/category/add"> 添加内容</a></li>
    </ul>
</div>
<div class="panel admin-panel">
    <table class="table table-hover text-center">
        <tr>
            <th>序号</th>
            <th>名称</th>
            <th>状态</th>
            <th>图片</th>
            <th>操作</th>
        </tr>
        <c:forEach items="${categories}" var="category" varStatus="abc">
            <tr id="tr${category.id}">
                <td>${abc.index+1}</td>
                <td>
                    <a href="${pageContext.request.contextPath}/admin/category/get?id=${category.id}">${category.name}</a>
                </td>
                <td>
                    禁用<input type="radio" class="${category.id}" name="${category.id}status" value="0" ${category.status ==0 ?"checked":""} onchange="radioValue(this)"><br>
                    启用<input type="radio" class="${category.id}" name="${category.id}status" value="1" ${category.status ==1 ?"checked":""} onchange="radioValue(this)">
                </td>
                <td><img src="${category.photo.img}" height="50" width="50"></td>
                <td>
                    <a class="button border-main" href="${pageContext.request.contextPath}/admin/category/upd?id=${category.id}">更新</a>
                    <a class="button border-red" href="javascript:void(0);"
                       onclick="del(${category.id})">删除</a>
                </td>
            </tr>
        </c:forEach>
    </table>
</div>
    <script>
        function radioValue(node) {
            $.ajax({
                url: "${pageContext.request.contextPath}/admin/category/changeStatus",
                type: "get",
                data: {"id": node.className, "status": node.value},
                success:function (data) {
                    if(data == "true") {
                        alert("修改成功");
                    }
                },
                error: function () {
                    alert("修改失败");
                }
            })
        }

        function del(id) {
            if(confirm('删除后不可恢复')){
                $.ajax({
                    url:"${pageContext.request.contextPath}/admin/category/del",
                    type:"get",
                    data:{"id":id},
                    success:function (data) {
                        if(data == "true"){
                            alert("删除成功");
                           // $("#tr" + id).remove();
                            window.location.reload();

                        }else
                        if(data == "false"){
                            alert("删除失败,正在使用中，不能删除");
                        }
                    },
                    error: function () {
                        alert("删除失败");
                    }
                })
            }else {
                return false;
            }
        }
    </script>
</div>
</body>
</html>
